package com.haier.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@Configuration("defaultFastjsonConfig")
@ConditionalOnClass(com.alibaba.fastjson.JSON.class)
@ConditionalOnMissingBean(FastJsonHttpMessageConverter.class)
@ConditionalOnWebApplication
public class DefaultFastjsonConfig {

	@Bean
	public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		converter.setFastJsonConfig(fastjsonConfig());
		converter.setSupportedMediaTypes(getSupportedMediaType());
		return converter;
	}

	/**
	 * fastjson的配置
	 */
	public FastJsonConfig fastjsonConfig() {
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteEnumUsingToString, SerializerFeature.DisableCircularReferenceDetect);
//		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		// 解决Long转json精度丢失的问题
		// SerializeConfig serializeConfig = SerializeConfig.globalInstance;
		// serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
		// serializeConfig.put(Long.class, ToStringSerializer.instance);
		// serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
		// fastJsonConfig.setSerializeConfig(serializeConfig);
//		SimplePropertyPreFilter simplePropertyPreFilter = new SimplePropertyPreFilter(IPage.class);
//		simplePropertyPreFilter.getExcludes().add("current");// 添加排除属性
//		simplePropertyPreFilter.getExcludes().add("pages");// 添加排除属性
//		simplePropertyPreFilter.getExcludes().add("searchCount");// 添加排除属性
//		simplePropertyPreFilter.getExcludes().add("size");// 添加排除属性
//		simplePropertyPreFilter.getExcludes().add("orders");// 添加排除属性
//		fastJsonConfig.setSerializeFilters(simplePropertyPreFilter);
		return fastJsonConfig;
	}

	/**
	 * 支持的mediaType类型
	 */
	public List<MediaType> getSupportedMediaType() {
		ArrayList<MediaType> mediaTypes = new ArrayList<>();
		mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		return mediaTypes;
	}

}
